package com.sunlands.zlcx.usercenter.controller;

import com.sunlands.zlcx.usercenter.common.annotation.AutoLog;
import com.sunlands.zlcx.usercenter.exception.GlobalExceptionHandler;
import com.sunlands.zlcx.usercenter.exception.PlatformException;
import com.sunlands.zlcx.usercenter.service.DurationSummaryServiceImpl;
import com.sunlands.zlcx.usercenter.util.DateUtil;
import com.sunlands.zlcx.usercenter.vo.*;
import com.sunlands.zlcx.usercenter.vo.response.BusinessResult;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

import static com.sunlands.zlcx.usercenter.common.annotation.AutoLog.Level.DEBUG;


/**
 * @author zhaocg
 * @create 2019/8/7 12:37
 */
@RestController
@RequestMapping(value = "/summary")
@Slf4j
@Api(tags = "统计排行")
@ApiResponses(value = {@ApiResponse(code = 400, response = GlobalExceptionHandler.class, message = "数据校验失败"), @ApiResponse(code = 500, response = GlobalExceptionHandler.class, message = "内部错误")})
public class DurationSummaryController extends BaseController {


    @Autowired
    private DurationSummaryServiceImpl durationSummaryService;

    @AutoLog( logLevel= DEBUG)
    @GetMapping(value = "/personal")
    @ApiOperation(value = "获取用户当天打卡信息")
    public BusinessResult getDurationSummary(@ApiParam(required = true, value = "用户ID") @RequestParam Long userId ){
        try {

            if ( userId == null ){
                return BusinessResult.createInstance(CommonResultMessage.PARAMS_NOT_INVALIDE, userId);
            }

            PersonalShowSummaryVO showSummaryVO = durationSummaryService.getDurationSummary(userId);

            return BusinessResult.createSuccessInstance(showSummaryVO);

        }catch (PlatformException e) {
            throw e;
        } catch (Exception e) {
            log.error("user query summer error , ex = {}" , e);
            return BusinessResult.createInstance(CommonResultMessage.UNKOWN_ERROR, "未知异常");
        }

    }

    @AutoLog( logLevel= DEBUG)
    @GetMapping(value = "/personal/yesterday")
    @ApiOperation(value = "获取用户昨天打卡记录")
    public BusinessResult findDurationSummaryListYesterday(@ApiParam(required = true, value = "用户ID") @RequestParam Long userId){
        try {

            if ( userId == null ){
                return BusinessResult.createInstance(CommonResultMessage.PARAMS_NOT_INVALIDE, userId);
            }

            List<PersonalSummaryDateVO> summaryDateVOS = durationSummaryService.findDurationSummaryList(userId , "-1");

            return BusinessResult.createSuccessInstance(summaryDateVOS);

        }catch (PlatformException e) {
            throw e;
        } catch (Exception e) {
            log.error("" , e);
            return BusinessResult.createInstance(CommonResultMessage.UNKOWN_ERROR, "未知异常");
        }

    }

    @AutoLog( logLevel= DEBUG)
    @GetMapping(value = "/personal/week")
    @ApiOperation(value = "获取用户近7天打卡记录")
    public BusinessResult findDurationSummaryListWeek(@ApiParam(required = true, value = "用户ID") @RequestParam Long userId){
        try {

            if ( userId == null ){
                return BusinessResult.createInstance(CommonResultMessage.PARAMS_NOT_INVALIDE, userId);
            }

            List<PersonalSummaryDateVO> summaryDateVOS = durationSummaryService.findDurationSummaryList(userId , "-7");

            return BusinessResult.createSuccessInstance(summaryDateVOS);

        }catch (PlatformException e) {
            throw e;
        } catch (Exception e) {
            log.error("" , e);
            return BusinessResult.createInstance(CommonResultMessage.UNKOWN_ERROR, "未知异常");
        }

    }

    @AutoLog( logLevel= DEBUG)
    @GetMapping(value = "/personal/month")
    @ApiOperation(value = "获取用户近30天打卡记录")
    public BusinessResult findDurationSummaryListMonth(@ApiParam(required = true, value = "用户ID") @RequestParam Long userId){
        try {

            if ( userId == null ){
                return BusinessResult.createInstance(CommonResultMessage.PARAMS_NOT_INVALIDE, userId);
            }

            List<PersonalSummaryDateVO> summaryDateVOS = durationSummaryService.findDurationSummaryList(userId , "-30");

            return BusinessResult.createSuccessInstance(summaryDateVOS);

        }catch (PlatformException e) {
            throw e;
        } catch (Exception e) {
            log.error("" , e);
            return BusinessResult.createInstance(CommonResultMessage.UNKOWN_ERROR, "未知异常");
        }

    }


    @AutoLog( logLevel= DEBUG)
    @GetMapping(value = "/personal/summaryList")
    @ApiOperation(value = "根据日期查询用户打卡情况")
    public BusinessResult findDurationSummaryListByDates(@ApiParam(required = true, value = "用户ID") @RequestParam Long userId ,
                                                         @ApiParam(required = true, value = "查询起始时间") @RequestParam String startTime ,
                                                         @ApiParam(required = true, value = "查询结束时间") @RequestParam String endTime){
        try {

            if ( userId == null ) {
                return BusinessResult.createInstance(CommonResultMessage.PARAMS_NOT_INVALIDE, userId);
            }

            if ( StringUtils.isBlank(startTime) ) {
                return BusinessResult.createInstance(CommonResultMessage.PARAMS_NOT_INVALIDE, startTime);
            }

            if ( StringUtils.isBlank(endTime) ) {
                return BusinessResult.createInstance(CommonResultMessage.PARAMS_NOT_INVALIDE, endTime);
            }

            List<PersonalSummaryDateVO> summaryDateVOS = durationSummaryService.findDurationSummaryListByDates(userId , DateUtil.strToDate(startTime) , DateUtil.strToDate(endTime));

            return BusinessResult.createSuccessInstance(summaryDateVOS);

        }catch (PlatformException e) {
            throw e;
        } catch (Exception e) {
            log.error("" , e);
            return BusinessResult.createInstance(CommonResultMessage.UNKOWN_ERROR, "未知异常");
        }

    }

    @AutoLog( logLevel= DEBUG)
    @GetMapping(value = "/personal/leaderBoard/day")
    @ApiOperation(value = "查看打卡用户当日排行榜")
    public BusinessResult getLeaderBoardDurationSummaryDay(@ApiParam(required = true, value = "用户ID") @RequestParam Long userId){
        try {

            if ( userId == null ) {
                return BusinessResult.createInstance(CommonResultMessage.PARAMS_NOT_INVALIDE, userId);
            }

            DurationShowSummaryVO showSummaryVO = durationSummaryService.getLeaderBoardDurationSummary(userId , "-2");

            return BusinessResult.createSuccessInstance(showSummaryVO);

        }catch (PlatformException e) {
            throw e;
        } catch (Exception e) {
            log.error("" , e);
            return BusinessResult.createInstance(CommonResultMessage.UNKOWN_ERROR, "未知异常");
        }

    }

    @AutoLog( logLevel= DEBUG)
    @GetMapping(value = "/personal/leaderBoard/week")
    @ApiOperation(value = "查看打卡用户近7天排行榜")
    public BusinessResult getLeaderBoardDurationSummaryWeek(@ApiParam(required = true, value = "用户ID") @RequestParam Long userId){
        try {

            if ( userId == null ) {
                return BusinessResult.createInstance(CommonResultMessage.PARAMS_NOT_INVALIDE, userId);
            }

            DurationShowSummaryVO showSummaryVO = durationSummaryService.getLeaderBoardDurationSummary(userId , "-8");

            return BusinessResult.createSuccessInstance(showSummaryVO);

        }catch (PlatformException e) {
            throw e;
        } catch (Exception e) {
            log.error("" , e);
            return BusinessResult.createInstance(CommonResultMessage.UNKOWN_ERROR, "未知异常");
        }

    }


}
